package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	import org.aswing.ASColor;
	import org.aswing.Container;
	import org.aswing.JButton;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JWindow;
	import org.aswing.SoftBoxLayout;
	
	
	public class tdMaker extends Sprite
	{
		
		private var m_sptCamera:Sprite;
		private var m_lblTip:JLabel;
		private var m_btnRecord:JButton;
		private var m_bIsRecording:Boolean;
		
		private var m_connection:NetConnection;
		private var m_stream:NetStream;
		private var m_responder:Responder;
		
		private var video:Video;
		private var cam:Camera;
		private var mic:Microphone;
		
		
		public function tdMaker()
		{
			createUI();
			var url:String = new String("rtmp://localhost/red5SimpleServer/");
			connect(url);
		}
		
		private function showTip(message:String):void {
			if(m_lblTip!=null && message!=null && message.length!=0) {
				m_lblTip.setForeground(ASColor.RED);
				m_lblTip.setText(message);
			}
		}
		
		private function clearTip():void {
			if(m_lblTip!=null) {
				m_lblTip.setText("");
			}
		}
		
		private function createUI() {
			m_sptCamera = new Sprite();
			addChild(m_sptCamera);
			
			var wndCamera:JWindow = new JWindow(m_sptCamera);
			
			// 创建摄像头的控制面板
			var pnlCamera:JPanel = new JPanel();
			pnlCamera.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 2, SoftBoxLayout.LEFT));
			
			m_lblTip = new JLabel();
			pnlCamera.append(m_lblTip);
			showTip("正在检测摄像头和麦克风...");
			
			// 启用摄像头	
			cam = Camera.getCamera();
			if(cam==null) {
				if(Camera.names.length==0) 
					showTip("您的机器没有安装摄像头！");
				else 
					showTip("摄像头已经被其他程序占用，请先关闭占用的程序!");
				return;
			}
			
			
			cam.setQuality(144000, 85);
			cam.setMode(320, 240, 15);
			cam.setKeyFrameInterval(60);
			video = new Video();
			video.attachCamera(cam);
			
			var pnlVideo:JPanel = new JPanel();
			pnlVideo.addChild(video);
			pnlVideo.setPreferredHeight(240);
			pnlCamera.append(pnlVideo);
			
			//启用麦克风
			mic = Microphone.getMicrophone();
			if(mic==null) {
				if(Microphone.names.length==0) 
					showTip("您的机器没有安装麦克风！");
				else 
					showTip("麦克风已经被其他程序占用，请先关闭占用的程序!");
				return;
			}
			mic.setSilenceLevel(0, -1);
			mic.gain=80;
			mic.setLoopBack(true);
			
			
			showTip("摄像头和麦克风已经启用");
			
			m_btnRecord = new JButton("开始录制");
			m_btnRecord.addEventListener(MouseEvent.CLICK, onBtnRecordClick);
			pnlCamera.append(m_btnRecord);
			
			
			wndCamera.setContentPane(pnlCamera);
			wndCamera.setActive(true);
			wndCamera.setSizeWH(320, 300);
			wndCamera.show();
		}
		
		private function connect(url:String):void {
			m_responder = new Responder(onResult, onStatus);
			m_connection = new NetConnection();
			m_connection.objectEncoding=ObjectEncoding.AMF3;
			m_connection.client = this;
			m_connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			m_connection.connect(url,true);
		}
		
		private function publishStream():void {
			if(m_connection.connected) {
				m_stream = new NetStream(m_connection);
				m_stream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
				m_stream.attachCamera(cam);
				m_stream.attachAudio(mic);
				m_stream.publish("mystream", "live");
			}	
		}
		
		private function onNetStatus(e : NetStatusEvent):void {
			if(e.info.code=="NetConnection.Connect.Success")
				publishStream();	
			
			trace(e.info.code)
		}
		
		
		private function onResult(data:Object):void {
			trace("recv result="+data);
		}
		
		private function onStatus(status:Object):void {
			trace("recv status="+status);
		}
		
		private function onBtnRecordClick(e:MouseEvent):void {
			if(m_btnRecord.getText()=="开始录制") {
				m_bIsRecording=true;
				showTip("正在录制...");
				m_connection.call("startRecord", m_responder, "mystream");
				m_btnRecord.setText("停止录制");
			} else if(m_btnRecord.getText()=="停止录制") {
				m_bIsRecording=false;
				showTip("停止录制...");
				m_connection.call("stopRecord", m_responder, "mystream");
				m_btnRecord.setText("开始录制");
			}
		}
	}
}